﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;

using TournamentDatabase.DialogBoxes.Edit;
using TournamentDatabase.Objects;
using TournamentDatabase.Database;
using TournamentDatabase.Singletons;

namespace TournamentDatabase.DialogBoxes.Select
{
	/// <summary>
	/// Interaction logic for MemberSelectDialogBox.xaml
	/// </summary>
	public partial class MemberSelectDialogBox : Window
	{
		Member m_selectedMember;
		List<Member> m_memberList;
		string m_currentSearch;

		public List<Member> MemberList
		{
			get { return m_memberList; }
			set { m_memberList = value; }
		}

		public Member SelectedMember
		{
			get { return m_selectedMember; }
			set { m_selectedMember = value; }
		}

		public MemberSelectDialogBox()
		{
			InitializeComponent();

			Search();

			this.DataContext = this;
		}

		private void createMemberButton_Click(object sender, RoutedEventArgs e)
		{
			MemberEditDialogBox medb = new MemberEditDialogBox();

			medb.ShowDialog();

			if (medb.DialogResult.HasValue && medb.DialogResult.Value)
			{
				Search();
				e.Handled = true;
			}
		}

		private void selectButton_Click(object sender, RoutedEventArgs e)
		{
			if (m_selectedMember != null)
			{
				DialogResult = true;
			}
		}

		private void OnSearch(string searchText)
		{
			m_currentSearch = searchText;
			Search();
		}

		private void Search()
		{
			if (m_currentSearch == null || m_currentSearch.Length == 0)
			{
				m_memberList = DatabaseSingleton.GetDatabaseAccessor().QueryMember("", OrderBy.None, "");
			}
			else
			{
				string where = "name LIKE '%" + m_currentSearch + "%'";
				m_memberList = DatabaseSingleton.GetDatabaseAccessor().QueryMember(where, OrderBy.None, "");
			}
		}
	}
}
